package cqrtplm.listener;

import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.hustcad.plm.pdm.workflow.annotion.CustomListener;
import com.hustcad.plm.pdm.workflow.model.constant.ProcessConstant;
import com.ty.basic.session.RequestContext;
import cqrtplm.service.IRtCfgDspfDemandMainService;
import cqrtplm.service.SapIntegrityService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.util.Map;

/**
 * @description: 需求状态表流程审批
 * @author: zhengxiong.jiang
 * @createDate: 2025-08-07
 * @version: 1.0
 */
@Slf4j
@CustomListener(category = "润通", description = "需求状态表流程审批")
public class StatusInfoListener implements ExecutionListener {


    @SneakyThrows
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void notify(DelegateExecution execution) {
        long start = System.currentTimeMillis();
        String workFlowContainerId = execution.getProcessInstanceBusinessKey();
        log.info("----------润通----需求状态表创建流程审批------开始------workFlowContainerId:{}----->>>",
                 workFlowContainerId);
        if (StringUtils.isBlank(workFlowContainerId)) {
            workFlowContainerId = execution.getVariable(ProcessConstant.WORK_FLOW_CONTAINER_ID).toString();
        }
        String finalWorkFlowContainerId = workFlowContainerId;
        Map<String, Object> requestContext = RequestContext.currentContext();
        IRtCfgDspfDemandMainService rtCfgDspfDemandMainService = SpringUtil.getBean(IRtCfgDspfDemandMainService.class);
        // CompletableFuture.runAsync(() -> {
        //     RequestContext.attachContext(requestContext);
//        try {
            rtCfgDspfDemandMainService.workFlowXqsp(new BigInteger(finalWorkFlowContainerId));
//        } catch (Exception e) {
//            log.info("需求状态表流程审批");
//            log.error(ExceptionUtil.getMessage(e));
//            log.error(ExceptionUtil.stacktraceToString(e, 100000));
//        }
        // });
        log.info("----------润通----需求状态表创建流程审批,耗时：{}---->>>", System.currentTimeMillis() - start);
    }
}
